package com.example.run_game;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class LazyAdapter extends BaseAdapter {
 
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    private AchievementManager mAchievementMan;
    private DbManager db;
 
    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d, Context c) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        db = new DbManager(c);
        mAchievementMan = new AchievementManager(db);
    }
 
    public int getCount() {
        return data.size();
    }
 
    public Object getItem(int position) {
        return position;
    }
 
    public long getItemId(int position) {
        return position;
    }
 
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        String name = "";
        String achieved = "";
        HashMap<String, String> achievement = new HashMap<String, String>();
        achievement = data.get(position);
        TextView title =null;
        TextView description =null;
        TextView date =null;
        ImageView thumb_image=null;
        
        //if(convertView==null)
        //{
        	name = achievement.get(AchievementCentre.KEY_TITLE);
        	achieved = mAchievementMan.isAchieved(name);
        	if(achieved == "")
        	{
        		vi = inflater.inflate(R.layout.achievement_list_row, null);
        		title = (TextView)vi.findViewById(R.id.title); // title
                description = (TextView)vi.findViewById(R.id.description); // artist name
                date = (TextView)vi.findViewById(R.id.date); // duration
                thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        		date.setText("Locked");
        	}
        	else
        	{
        		vi = inflater.inflate(R.layout.achievement_list_row_achieved, null);
        		title = (TextView)vi.findViewById(R.id.title); // title
                description = (TextView)vi.findViewById(R.id.description); // artist name
                date = (TextView)vi.findViewById(R.id.date); // duration
                thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        		date.setText(achieved);
        	}              
        //}
        // Setting all values in listview
        title.setText(achievement.get(AchievementCentre.KEY_TITLE));
        description.setText(achievement.get(AchievementCentre.KEY_DESCRIPTION));
		imageLoader.DisplayImage(activity.getApplicationContext(),achievement.get(AchievementCentre.KEY_THUMB_PATH), thumb_image);
          	
        return vi;
    }
 
}